package cn.woblog.android.http;

import android.annotation.SuppressLint;
import android.content.Context;

@SuppressLint("NewApi")
public class Http {
	public static final int OK = 1;
	private static AsyncHttpClient  client = new AsyncHttpClient();
//	private final static OkHttpClient client;
//	static {
//		client = new OkHttpClient();
//		client.networkInterceptors().add(new StethoInterceptor());
//		client.interceptors().add(new LoggingInterceptor());
//		client.setConnectTimeout(10, TimeUnit.SECONDS);
//		client.setWriteTimeout(10, TimeUnit.SECONDS);
//		client.setReadTimeout(30, TimeUnit.SECONDS);
//	}
//	private static Handler handler = new Handler() {
//		public void handleMessage(android.os.Message msg) {
//			switch (msg.what) {
//			case Const.MSG_SHOW_OFFLINE_INFO:
//				showOfflineToast();
//				break;
//
//			default:
//				break;
//			}
//		};
//	};
//	private static Map<String, Call> requestCaches = new HashMap<String, Call>();
//	private static CacheControl cacheControl = new CacheControl();
	private static Context context;

	public static Context getContext() {
		return context;
	}

//	protected static void showOfflineToast() {
//		ToastAlone.showToast((Activity) context, "当前网络错误,只能使用离线", 0);
//	}
//
	public static void setContext(Context context) {
		Http.context = context;
	}
//
//	public static CacheControl getCacheControl() {
//		return cacheControl;
//	}
//
//	public static void setCacheControl(CacheControl cacheControl) {
//		Http.cacheControl = cacheControl;
//	}
//
//	public static void addCache(String key, Call value) {
//		requestCaches.put(key, value);
//	}
//
//	public static void cancel(String tag) {
//		Call call = requestCaches.get(tag);
//		if (call != null && !call.isCanceled()) {
//			call.cancel();
//			requestCaches.remove(tag);
//		}
//	}
//
//	public static void removeCall(String tag) {
//		requestCaches.remove(tag);
//	}
//
//	public static <T> void post(final String url, final EncodeForm form, final ICallback<T> callback) {
//		post(url, form, callback, null);
//	}
//
//	public static <T> void post(final String url, final EncodeForm form, final ICallback<T> callback, String tag) {
//		Request request = null;
//		if (form == null) {
//			request = new Request.Builder().url(url).post(null).build();
//		} else {
//			RequestBody formBody = form.build();
//			request = new Request.Builder().url(url).post(formBody).build();
//		}
//		excuteTask(createRequestTask(callback, tag, request));
//	}
//
//	public static <T> void get(final String url, final EncodeForm form, final ICallback<T> callback, String tag) {
//		String formUrl = null;
//		if (form == null) {
//			formUrl = url;
//		} else {
//			formUrl = form.buildString(url);
//		}
//		Request request = new Request.Builder().url(formUrl).build();
//		excuteTask(createRequestTask(callback, tag, request));
//	}
//
//	public static <T> void get(final String url, final EncodeForm form, final ICallback<T> callback) {
//		get(url, form, callback, null);
//	}
//
//	private static <T> RequestTask<T> createRequestTask(final ICallback<T> callback, String tag, Request request) {
//		return new RequestTask<T>(client, request, callback, tag);
//	}
//
//	private static void excuteTask(AsyncTask<Void, Integer, Object[]> task) {
//		if (Build.VERSION.SDK_INT < 11) {
//			task.execute((Void) null);
//		} else {
//			task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
//		}
//	}
//
//	public static Handler getHandler() {
//		return handler;
//	}

	//new async 2015-06-03 11:18:28
	public static RequestHandle post(final String url, final RequestParams params, final ResponseHandlerInterface responseHandlerInterface) {
		//post(url, form, callback, null);
		if (params==null){
			return client.post(url,responseHandlerInterface);
		}
		return 	client.post(url,params,responseHandlerInterface);
	}
	public static RequestHandle post(final String url,final ResponseHandlerInterface responseHandlerInterface) {
		return 	post(url, responseHandlerInterface);
	}
	public static RequestHandle get(final String url, final RequestParams params, final ResponseHandlerInterface responseHandlerInterface){
		if (params==null){
			return client.get(url, responseHandlerInterface);
		}
		return 	client.get(url,params,responseHandlerInterface);
	}
	public static RequestHandle get(final String url, final ResponseHandlerInterface responseHandlerInterface){
		return 	get(url,responseHandlerInterface);
	}
}
